﻿using LoveEat.API.Read.Applications.Commands.Goods;
using LoveEat.API.Read.DTOs;
using LoveEat.Domain.Goods;
using LoveEat.ErrorCode;
using LoveEat.Infrastructure.Interfaces;
using MediatR;
using Microsoft.AspNetCore.Server.HttpSys;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Update.Internal;
using Microsoft.VisualBasic;

namespace LoveEat.API.Read.Applications.CommandHandlers.Goods
{
    /// <summary>
    /// 商品查询请求命令
    /// </summary>
    public class GoodsQueryCommandHandler : IRequestHandler<GoodsQueryCommand, APIResult<APIPaging<GoodsDTO>>>
    {
        private readonly IBaseRespository<GoodsNameModel> nameRep;
        private readonly IBaseRespository<GoodsModel> goodsRep;

        public GoodsQueryCommandHandler(IBaseRespository<GoodsNameModel> nameRep, 
            IBaseRespository<GoodsModel> goodsRep)
        {
            this.nameRep = nameRep;
            this.goodsRep = goodsRep;
        }

        public  Task<APIResult<APIPaging<GoodsDTO>>> Handle(GoodsQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaging<GoodsDTO>> result = new APIResult<APIPaging<GoodsDTO>>();
            result.Code=APIResultCode.查询成功;
            result.Message=APIResultCode.查询成功.ToString();

            var res = from a in goodsRep.GetAll()
                      join b in nameRep.GetAll() on a.GoodsNameId equals b.GoodsNameId
                      where !a.IsDelete
                      select new GoodsDTO
                      {
                          GoodsId=a.GoodsId,
                          GoodsNameId=a.GoodsNameId,
                          Production=a.Production,
                          LifeDay= a.LifeDay,
                          Pronumber=a.Pronumber,
                          ProCommand=a.ProCommand,
                          Path=a.Path,
                          Information=a.Information,
                          CreateName=a.CreateName,
                          CreateTime=a.CreateTime,
                          UpdateName=a.UpdateName,
                          AquacultureId=a.AquacultureId,
                          slaughterId=a.slaughterId,
                          DeepId=a.DeepId,
                          GoodsNameName=b.GoodsNameName,
                      };
            //商品名称模糊查询
            if (!string.IsNullOrEmpty(request.GoodsNameName))
            {
                res = res.Where(x => x.GoodsNameName.Contains(request.GoodsNameName));
            }
            //生产批次查询
            if (!string.IsNullOrEmpty(request.Production))
            {
                res = res.Where(x => x.Production.Contains(request.Production));
            }
            //保质期（精确查询）
            if (request.LifeDay != 0)
            {
                res=res.Where(x=>x.LifeDay==request.LifeDay);
            }
            //生产许可证号
            if (request.Pronumber != null)
            {
                res=res.Where(x=>x.Pronumber==request.Pronumber);
            }
            //生产企业
            if (!string.IsNullOrEmpty(request.ProCommand))
            {
                res=res.Where(x=>x.ProCommand.Contains(request.ProCommand));
            }
            //创建人
            if (request.CreateName != null)
            {
                res=res.Where(x=>x.CreateName==request.CreateName);
            }
            //创建时间
            if (!string.IsNullOrEmpty(request.CreateTime))
            {
                res=res.Where(x=>x.CreateTime.ToString().Substring(0,10)== request.CreateTime);
            }
            //修改人  UpdateName
            if (!string.IsNullOrEmpty(request.UpdateName))
            {
                res = res.Where(x => x.UpdateName == request.UpdateName);
            }

            //总条数
            var  totalCount=res.Count();
            //总页数
            var totalpage = (int)Math.Ceiling(totalCount * 1.0 / request.Pagsize);
            //排序
            res = res.OrderByDescending(x => x.GoodsId).Skip((request.Pagindex - 1) * request.Pagsize).Take(request.Pagsize);

            APIPaging<GoodsDTO> paging = new APIPaging<GoodsDTO>();
            paging.TotalCount = totalCount;
            paging.TotalPage = totalpage;
            paging.Fdata=res.ToList();
            result.Data = paging;

            return  Task.FromResult(result);
        }
    }
}
